package cn.powertime.evaluation.app.controller;


import cn.powertime.evaluation.Constants;
import cn.powertime.evaluation.entity.CircleUser;
import cn.powertime.evaluation.entity.CpsCircle;
import cn.powertime.evaluation.entity.DbqAnswer;
import cn.powertime.evaluation.entity.DbqQuestion;
import cn.powertime.evaluation.exception.FileServerException;
import cn.powertime.evaluation.service.CircleUserService;
import cn.powertime.evaluation.service.DbqAnswerService;
import cn.powertime.evaluation.service.DbqQuestionService;
import cn.powertime.evaluation.shiro.ShiroUtils;
import cn.powertime.evaluation.vo.*;
import cn.powertime.evaluation.web.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.powertime.id.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 问题表 前端控制器
 * </p>
 *
 * @author liqi
 * @since 2019-09-23
 */
@RestController
@RequestMapping("/app/dbq-question")
@Api(value = "/app/dbq-question", tags = "等保圈接口", produces = MediaType.ALL_VALUE)
public class AppDbqQuestionController extends BaseController {

    @Autowired
    private DbqQuestionService dbqQuestionService;

    @Autowired
    private DbqAnswerService dbqAnswerService;

    @Autowired
    private CircleUserService circleUserService;


    @RequestMapping("/list")
    @ApiOperation(value = "首页问题列表",response = DbqQuestionListVo.class)
    public  Object select(@Validated @RequestBody PageVoS  vo){
        Page page1 = PageHelper.startPage(vo.getPage(),vo.getSize());
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        List<DbqQuestionVo> list=  dbqQuestionService.questionDetailsList(userInfo.getId());
        PageInfo pageInfo = new PageInfo(list);
        List<DbqQuestionListVo> listVos = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            QueryWrapper<DbqAnswer> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("status", Constants.STATUS_NORMAL);
            queryWrapper.eq("question_id",list.get(i).getId());
            int s=dbqAnswerService.count(queryWrapper);
            DbqQuestionListVo dbqQuestionListVo = new DbqQuestionListVo();
            dbqQuestionListVo.setCount(s);
            dbqQuestionListVo.setContent(list.get(i).getContent());
            dbqQuestionListVo.setId(list.get(i).getId());
            dbqQuestionListVo.setUserId(list.get(i).getUserId());
            dbqQuestionListVo.setTitle(list.get(i).getTitle());
            dbqQuestionListVo.setNewsWarePv(list.get(i).getScore());
            dbqQuestionListVo.setCreateTime(list.get(i).getCreateTime());
            dbqQuestionListVo.setFileId(list.get(i).getFileId());
            dbqQuestionListVo.setAvatar(list.get(i).getAvatar());
            dbqQuestionListVo.setNickName(list.get(i).getNickName());
            listVos.add(dbqQuestionListVo);
        }
        return  success1(listVos,pageInfo.getPages(),pageInfo.getPageSize(), page1.getTotal());
    }

    @PostMapping("/answer")
    @ApiOperation(value = "等保圈我的回答列表",response = DbqQuestionVo.class)
    public Object answer(@Validated @RequestBody  PageVoS vo){
        Page page1 = PageHelper.startPage(vo.getPage(),vo.getSize());
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        List<DbqAnswerVo> list= dbqAnswerService.answerQuery(userInfo.getId());
        PageInfo pageInfo = new PageInfo(list);
        List<DbqQuestionListVo> listVos = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            QueryWrapper<DbqAnswer> dbqQuestionQueryWrapper1 = new QueryWrapper<>();
            dbqQuestionQueryWrapper1.eq("status", Constants.ALLOW_DEL);
            dbqQuestionQueryWrapper1.eq("question_id",list.get(i).getQuestionId());
            int s=dbqAnswerService.count(dbqQuestionQueryWrapper1);
            List<DbqQuestionVo> list1=  dbqQuestionService.questionQuery(list.get(i).getQuestionId());
            DbqQuestionListVo dbqQuestionListVo = new DbqQuestionListVo();
            dbqQuestionListVo.setContent(list1.get(0).getContent());
            dbqQuestionListVo.setCount(s);
            dbqQuestionListVo.setId(list1.get(0).getId());
            dbqQuestionListVo.setTitle(list1.get(0).getTitle());
            dbqQuestionListVo.setUserId(userInfo.getId());
            //dbqQuestionListVo.setUserId(1L);
            dbqQuestionListVo.setNickName(list1.get(0).getNickName());
            dbqQuestionListVo.setClassifiedGade(list1.get(0).getClassifiedGade());
            dbqQuestionListVo.setAvatar(list1.get(0).getAvatar());
            dbqQuestionListVo.setFileId(list1.get(0).getFileId());
            dbqQuestionListVo.setNewsWarePv(list1.get(0).getScore());
            dbqQuestionListVo.setCreateTime(list1.get(0).getCreateTime());
            dbqQuestionListVo.setFileId(list1.get(0).getFileId());
            listVos.add(dbqQuestionListVo);
        }
        //return  success(listVos);
        return  success1(listVos,pageInfo.getPages(),pageInfo.getPageSize(), page1.getTotal());
    }

    @PostMapping("/question")
    @ApiOperation(value = "等保圈我的提问列表",response = DbqQuestionVo.class)
    public Object question(@Validated @RequestBody PageVoS vo){
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        Page page1 = PageHelper.startPage(vo.getPage(),vo.getSize());
        List<DbqQuestionVo> list=  dbqQuestionService.questionUserQuery(userInfo.getId());
        List<DbqQuestionListVo> listVos = new ArrayList<>();
        PageInfo pageInfo = new PageInfo(list);
        DbqQuestionListVo dbqQuestionListVo ;
        for (int i=0;i<list.size();i++){
            QueryWrapper<DbqAnswer> dbqQuestionQueryWrapper1 = new QueryWrapper<>();
            dbqQuestionQueryWrapper1.eq("status", Constants.STATUS_NORMAL);
            dbqQuestionQueryWrapper1.eq("question_id",list.get(i).getId());
            List<DbqAnswer>  s=dbqAnswerService.list(dbqQuestionQueryWrapper1);
            dbqQuestionListVo =new DbqQuestionListVo();
            dbqQuestionListVo.setContent(list.get(i).getContent());
            dbqQuestionListVo.setCount(s.size());
            dbqQuestionListVo.setId(list.get(i).getId());
            dbqQuestionListVo.setTitle(list.get(i).getTitle());
            dbqQuestionListVo.setUserId(userInfo.getId());
            //dbqQuestionListVo.setUserId(1L);
            dbqQuestionListVo.setNickName(list.get(i).getNickName());
            dbqQuestionListVo.setClassifiedGade(list.get(i).getClassifiedGade());
            dbqQuestionListVo.setNewsWarePv(list.get(i).getScore());
            dbqQuestionListVo.setCreateTime(list.get(i).getCreateTime());
            dbqQuestionListVo.setFileId(list.get(i).getFileId());
            dbqQuestionListVo.setAvatar(list.get(i).getAvatar());
            listVos.add(dbqQuestionListVo);
        }
        //return  success(listVos);
        return  success1(listVos,pageInfo.getPages(),pageInfo.getPageSize(), page1.getTotal());
    }

    @PostMapping("/details")
    @ApiOperation(value = "等保圈我的问题详情",response = DbqAnswerVo.class)
    public  Object details(@Validated @RequestBody QuestionDetailsVo vo){
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        String s1="1";//
     /*   QueryWrapper<DbqQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", Constants.ALLOW_DEL);
        queryWrapper.eq("id",vo.getQuestionId());
        queryWrapper.eq("user_id",userInfo.getId());
        List<DbqQuestion> questionList= dbqQuestionService.list(queryWrapper);
        if(questionList.size()>0){
            s1="2";
        }*/
        QueryWrapper<DbqAnswer> dbqQuestionQueryWrapper1 = new QueryWrapper<>();
        dbqQuestionQueryWrapper1.eq("status", Constants.ALLOW_DEL);
        dbqQuestionQueryWrapper1.eq("question_id",vo.getQuestionId());
        dbqQuestionQueryWrapper1.eq("user_id",userInfo.getId());
        List<DbqAnswer> list1=dbqAnswerService.list(dbqQuestionQueryWrapper1);
        AppAnswerVo vo1 = new AppAnswerVo();
        vo1.setQuestionId(vo.getQuestionId());
        vo1.setUserId(userInfo.getId());
        List<DbqAnswerVo> list=dbqAnswerService.answerUserList(vo1);
        if(list1.size()>0){
            s1="2";
            for(int i = 0 ;i<list.size();i++){
                if(list.get(i).getUserId().equals(list1.get(0).getUserId())){
                    list.get(i).setFine(2);
                }
            }
        }
        DbqQuestion dbqQuestion = new DbqQuestion();
        dbqQuestion.setId(vo.getQuestionId());
        int score=vo.getScore();
        score++;
        dbqQuestion.setScore(score);
        boolean b=dbqQuestionService.ownUpdateById(dbqQuestion);
        if (!b) {
            return error("浏览量增加失败");
        }
        return  success2(list,s1);
    }

    @PostMapping("/questionList")
    @ApiOperation(value = "等保圈我的问题详细列表",response = DbqAnswerVo.class)
    public  Object questionList(){
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        List<DbqQuestionVo> list=dbqQuestionService.questionDetailsList(userInfo.getId());
        List<DbqQuestionListVo> listVos = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            QueryWrapper<DbqAnswer> dbqQuestionQueryWrapper1 = new QueryWrapper<>();
            dbqQuestionQueryWrapper1.eq("status", Constants.ALLOW_DEL);
            dbqQuestionQueryWrapper1.eq("question_id",list.get(i).getId());
            int  s=dbqAnswerService.count(dbqQuestionQueryWrapper1);
            DbqQuestionListVo dbqQuestionListVo = new DbqQuestionListVo();
            dbqQuestionListVo.setContent(list.get(i).getContent());
            dbqQuestionListVo.setCount(s);
            dbqQuestionListVo.setId(list.get(i).getId());
            dbqQuestionListVo.setTitle(list.get(i).getTitle());
            dbqQuestionListVo.setUserId(userInfo.getId());
            //dbqQuestionListVo.setUserId(1L);
            dbqQuestionListVo.setNickName(list.get(i).getNickName());
            dbqQuestionListVo.setFileId(list.get(i).getFileId());
            dbqQuestionListVo.setAvatar(list.get(i).getAvatar());
            dbqQuestionListVo.setClassifiedGade(list.get(i).getClassifiedGade());
            dbqQuestionListVo.setNewsWarePv(list.get(i).getScore());
            dbqQuestionListVo.setCreateTime(list.get(i).getCreateTime());
            dbqQuestionListVo.setFileId(list.get(i).getFileId());
            listVos.add(dbqQuestionListVo);
        }
        return  success(listVos);
    }

    @PostMapping("/questionAdd")
    @ApiOperation(value = "等保圈我的提问")
    public Object questionAdd(@Validated @RequestBody DbqQuestion dbqQuestion){
        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        dbqQuestion.setId(IdWorker.getId());
        dbqQuestion.setUpdateTime(LocalDateTime.now());
        dbqQuestion.setCreateTime(LocalDateTime.now());
        dbqQuestion.setUserId(userInfo.getId());
        dbqQuestion.setStatus(Constants.STATUS_NORMAL);
        boolean b= dbqQuestionService.ownSave(dbqQuestion);
        if (b) {
            return success("成功");
        }
        return  error("失败");
    }

    @PostMapping("/answerAdd")
    @ApiOperation(value = "等保圈我的回答")
    public Object answerAdd(@Validated @RequestBody DbqAnswer dbqAnswer){

        ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<DbqAnswer> dbqQuestionQueryWrapper1 = new QueryWrapper<>();
        dbqQuestionQueryWrapper1.eq("status", Constants.ALLOW_DEL);
        dbqQuestionQueryWrapper1.eq("question_id",dbqAnswer.getQuestionId());
        dbqQuestionQueryWrapper1.eq("user_id",userInfo.getId());
        int  s=dbqAnswerService.count(dbqQuestionQueryWrapper1);
        if(s>0){
            return  error("此问题你以回答过不能在回答");
        }
        dbqAnswer.setId(IdWorker.getId());
        dbqAnswer.setUpdateTime(LocalDateTime.now());
        dbqAnswer.setCreateTime(LocalDateTime.now());
        dbqAnswer.setUserId(userInfo.getId());
        //dbqAnswer.setUserId(1L);
        dbqAnswer.setStatus( Constants.STATUS_CREDIT);
        boolean b=dbqAnswerService.ownSave(dbqAnswer);
        if (b) {
            return success("成功");
        }
        return  error("失败");
    }

    @PostMapping("/cicleList")
    @ApiOperation(value = "我的所有圈子")
    public Object circleUser(){
         ShiroUserInfo userInfo = ShiroUtils.getUserInfo();
         List<CpsCircle> circleUsers=circleUserService.circleUserList(userInfo.getId());
         return success(circleUsers);

    }

    @ApiOperation(value = "选为最佳答案")
    @PostMapping("/bestAnswer")
    //@ApiImplicitParam(name = "answerId", value = "答案id", required = true, dataType = "Long")
    public  Object bestAnswer(@Validated @RequestBody BestAnswerVo vo){
         QueryWrapper<DbqAnswer> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("status", Constants.ALLOW_DEL);
         queryWrapper.eq("answer_type" ,Constants.NO_ALLOW_DEL);
         queryWrapper.eq("question_id" ,vo.getQuestionId());
         int i=dbqAnswerService.count(queryWrapper);
         if(i>0){
             return error("已有最佳答案不能再选");
         }
         DbqAnswer dbqAnswer = new DbqAnswer();
         dbqAnswer.setAnswerType(Constants.NO_ALLOW_DEL);
         dbqAnswer.setId(vo.getAnswerId());
         boolean b=dbqAnswerService.ownUpdateById(dbqAnswer);
        if (b) {
            return success("成功");
        }
        return  error("失败");
    }
}
